//
//  GlobalMethod+UI.swift
//  全局方法 - UI
//
//  Created by mini on 2025/4/28.
//

import Foundation



extension GlobalMethod {
    
    // MARK: - 计算高度
    static func fetchHeightFromLabel(label: UILabel) -> CGFloat {
        return self.fetchHeightFromLabel(label: label, heightLimit: 100000)
    }
    
    static func fetchHeightFromLabel(label: UILabel, heightLimit: CGFloat) -> CGFloat {
        var attributedString: NSAttributedString
        if let labelText = label.text, !labelText.isEmpty {
            attributedString = NSAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: label.font!])
        } else {
            attributedString = NSAttributedString(string: "A", attributes: [NSAttributedString.Key.font: label.font!])
        }
        var rect: CGRect = attributedString.boundingRect(with: CGSizeMake(label.width, heightLimit), context: nil)
        var numHeight: CGFloat = rect.size.height + 1
        // 限制行数
        if label.numberOfLines != 0 {
            attributedString = NSAttributedString(string: "A", attributes: [NSAttributedString.Key.font: label.font!])
            rect = attributedString.boundingRect(with: CGSizeMake(label.width, heightLimit), options: .usesLineFragmentOrigin, context: nil)
            numHeight = (numHeight > CGFloat(label.numberOfLines)*rect.size.height) ? (CGFloat(label.numberOfLines)*rect.size.height) : numHeight
        }
        return ceil(numHeight)
    }
    
    static func fetchWidthFromLabel(label: UILabel) -> CGFloat {
        let strContent = isStr(label.text) ? (label.text) : ""
        let font = label.font
        let attributeString = NSAttributedString(string: strContent!, attributes: [NSAttributedString.Key.font: font!])
        let rect = attributeString.boundingRect(with: CGSizeMake(1000, 1000), options: .usesLineFragmentOrigin, context: nil)
        return ceil(rect.size.width)
    }
    
    static func fetchWidthFromButton(button: UIButton) -> CGFloat {
        let label = UILabel()
        label.font = button.titleLabel!.font
        label.text = button.titleLabel!.text
        return self.fetchWidthFromLabel(label: label)
    }
    
    static func fetchHeightFromFont(fontNum: CGFloat) -> CGFloat {
        let attributeString = NSAttributedString(string: "A", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: (fontNum))])
        let rect = attributeString.boundingRect(with: CGSizeMake(CGFloat(Int.max), CGFloat(Int.max)), options: .usesLineFragmentOrigin, context: nil)
        return rect.size.height
    }
    
    static func fetchViewWithHeight(_ height: CGFloat) -> UIView {
        let view = UIView()
        view.height = height
        return view
    }
    
    // MARK: - 设置UILabel
    static func resetLabel(
        label: UILabel,
        text: String,
        widthLimit: CGFloat) {
        self.setLabel(label: label, widthLimit: widthLimit, numLines: label.numberOfLines, fontNum: label.font.pointSize, textColor: label.textColor, aligent: label.textAlignment, text: text, bgColor: label.backgroundColor)
    }
    static func resetLabel(
        label: UILabel,
        text: String,
        isWidthLimit: Bool) {
        if isWidthLimit {
            self.resetLabel(label: label, text: text, widthLimit: label.width)
        } else {
            self.resetLabel(label: label, text: text, widthLimit: 0)
        }
    }
    static func resetLabel(
        label: UILabel,
        attributeString: NSAttributedString,
        widthLimit: CGFloat) {
        label.attributedText = attributeString
        let rect: CGRect = attributeString.boundingRect(with: CGSizeMake(widthLimit, 1000), options: .usesLineFragmentOrigin, context: nil)
        label.width = min(CGFloat(rect.width), widthLimit)
        // 限制行数
        label.height = self.fetchHeightFromLabel(label: label, heightLimit: CGFLOAT_MAX)
    }
    static func setLabel(
        label: UILabel,
        widthLimit: CGFloat,
        numLines: Int,
        fontNum: CGFloat,
        textColor: UIColor?,
        text: String) {
        self.setLabel(label: label, widthLimit: widthLimit, numLines: numLines, fontNum: fontNum, textColor: textColor, aligent: label.textAlignment, text: text, bgColor: .clear)
    }
    static func setLabel(
        label: UILabel,
        widthLimit: CGFloat,
        numLines: Int,
        fontNum: CGFloat,
        textColor: UIColor?,
        aligent: NSTextAlignment,
        text: String,
        bgColor: UIColor?) {
        self.setLabel(label: label, widthLimit: widthLimit, numLines: numLines, isBold: false, fontNum: fontNum, textColor: textColor, aligent: aligent, text: text, bgColor: bgColor)
    }
    static func setLabel(
        label: UILabel,
        widthLimit: CGFloat,
        numLines: Int,
        isBold: Bool,
        fontNum: CGFloat,
        textColor: UIColor?,
        aligent: NSTextAlignment,
        text: String,
        bgColor: UIColor?) {
        label.numberOfLines = numLines
        if isBold {
            label.font = UIFont.boldSystemFont(ofSize: fontNum)
        } else {
            label.font = UIFont.systemFont(ofSize: fontNum)
        }
            label.textColor = textColor == nil ? COLOR_LABEL() : textColor
        label.textAlignment = aligent
        label.backgroundColor = bgColor == nil ? UIColor.clear : bgColor
        label.text = UnPackStr(text)
        let widthMAX: CGFloat = self.fetchWidthFromLabel(label: label)
        if widthLimit != 0 {
            if widthMAX < widthLimit {
                label.width = widthMAX
            } else {
                label.width = widthLimit
            }
        } else {
            label.width = widthMAX
        }
        label.height = self.fetchHeightFromLabel(label: label)
    }

    
    
    

    
    
    // MARK: - 设置圆角
    static func setRoundView(view: UIView, color: UIColor, numRound: CGFloat, width: CGFloat) {
        view.layer.cornerRadius = numRound // 圆角设置
        view.layer.masksToBounds = true
        view.layer.borderWidth = width
        view.layer.borderColor = color.cgColor
    }
    
    
}
